<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-row :gutter="20">
            <el-col :span="12">
                <el-form-item label="学校名称" prop="school">
                    <el-input v-model="ruleForm.school" />
                </el-form-item>
                <el-form-item label="学习形式" prop="type">
                    <el-select v-model="ruleForm.type" placeholder="Select">
                        <el-option label="全日制" value="全日制" />
                        <el-option label="非全日制" value="非全日制" />
                    </el-select>
                </el-form-item>
                <el-form-item label="学历" prop="edu">
                    <el-select v-model="ruleForm.edu" placeholder="Select">
                        <el-option label="初中及以下" value="初中及以下" />
                        <el-option label="中专" value="中专" />
                        <el-option label="高中" value="高中" />
                        <el-option label="技校" value="技校" />
                        <el-option label="大学专科" value="大学专科" />
                        <el-option label="大学本科" value="大学本科" />
                    </el-select>
                </el-form-item>
                <el-form-item label="学位" prop="degree">
                    <el-input v-model="ruleForm.degree" />
                </el-form-item>
                <el-form-item label="专业" prop="major">
                    <el-input v-model="ruleForm.major" />
                </el-form-item>
                <el-form-item label="学位证书编号" prop="degree_numbers">
                    <el-input v-model="ruleForm.degree_numbers" />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="学制" prop="system">
                    <el-input v-model="ruleForm.system" />
                </el-form-item>
                <el-form-item label="入学时间" prop="in_date">
                    <el-date-picker v-model="ruleForm.in_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
                </el-form-item>
                <el-form-item label="毕业时间" prop="graduate_date">
                    <el-date-picker v-model="ruleForm.graduate_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
                </el-form-item>
                <el-form-item label="学历证书编号" prop="numbers">
                    <el-input v-model="ruleForm.numbers" />
                </el-form-item>
                <el-form-item label="证书文件" prop="cert">
                    <el-button type="primary" size="small" @click="onUploadFile('cert')"> 上传 </el-button>
                    <el-button :type="ruleForm.cert ? 'primary' : 'info'" size="small" @click="onShowFile(ruleForm.cert)"> 查看 </el-button>
                </el-form-item>
                <el-form-item label="学位证书" prop="degree_cert">
                    <el-button type="primary" size="small" @click="onUploadFile('degree_cert')"> 上传 </el-button>
                    <el-button :type="ruleForm.degree_cert ? 'primary' : 'info'" size="small" @click="onShowFile(ruleForm.degree_cert)"> 查看 </el-button>
                </el-form-item>
                <el-form-item label="学信网证明" prop="network_cert">
                    <el-button type="primary" size="small" @click="onUploadFile('network_cert')"> 上传 </el-button>
                    <el-button :type="ruleForm.network_cert ? 'primary' : 'info'" size="small" @click="onShowFile(ruleForm.network_cert)"> 查看 </el-button>
                </el-form-item>
            </el-col>
        </el-row>
    </el-form>
    <div class="mt20 text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
    </div>

    <el-dialog v-model="showUpload" title="文件">
        <component :is="uploadFile" :data="uploadData" @saveUpload="saveUpload" />
    </el-dialog>

    <el-dialog v-model="isShowFile" title="文件" width="80%" @close="onCloseFile">
        <component :is="showFile" :data="files"/>
    </el-dialog>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import http from '../../../../util/request';
import uploadFile from '@/components/uploadFile.vue'
import showFile from '@/components/showFile.vue'
const loading = ref<boolean>(false)
    const files = ref<string>('')
        const isShowFile = ref<boolean>(false)
const showUpload = ref<boolean>(false)
let uploadData:any = ref({prefix:'cert/'})

interface RuleForm {
    id: number|string
    user_id:number
    school: string
    numbers: string
    type: string
    edu: string
    degree: string
    major: string
    cert: string
    degree_cert:string
    network_cert:string
    in_date: string
    graduate_date: string
    degree_numbers:string
    system:string
} 

const formSize = ref<ComponentSize>('large')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id: 0,
    user_id:0,
    school: '/',
    numbers: '/',
    type: '/',
    edu: '/',
    degree: '/',
    major: '/',
    cert: '',
    degree_cert:'',
    network_cert:'',
    in_date: '',
    graduate_date: '',
    degree_numbers:'/',
    system:'/',
})

const onGetData = () => {
    if(!ruleForm.value.id){
        return
    }
    http.get('/userEdu/'+ruleForm.value.id).then((response:any)=>{
        ruleForm.value = response.data.data
    })
}

const props = defineProps<{
    user_id:number
    id:number
    type:number
}>()

watch(props,()=>{
    ruleForm.value.id = props.id
    ruleForm.value.user_id = props.user_id
    ruleForm.value.school = '/'
    ruleForm.value.numbers = '/'
    ruleForm.value.type = ''
    ruleForm.value.edu = ''
    ruleForm.value.degree = ''
    ruleForm.value.major = '/'
    ruleForm.value.cert = ''
    ruleForm.value.in_date = ''
    ruleForm.value.graduate_date = ''
    onGetData()
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    school: [
        { required: true, message: '请输入名学校称', trigger: 'blur' },
    ],
    type: [
        { required: true, message: '请输入学习形式', trigger: 'blur' },
    ],
    edu: [
        { required: true, message: '请选择学历', trigger: 'blur' },
    ],
    in_date: [
        { required: true, message: '请选择入学时间', trigger: 'blur' },
    ],
    graduate_date: [
        { required: true, message: '请选择毕业时间', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            let request:any = null
            if(ruleForm.value.id){
                request = http.patch('/user/edu',ruleForm.value)
            }else{
                request = http.put('/user/edu',ruleForm.value)
            }
            request.then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);return
                }
                ElMessage.success(response.data.msg);
                onSaveEdu()  
            })
                      
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onShowFile = (file:string) => {
    files.value = file
    isShowFile.value = true
}
const onCloseFile = () => {
    files.value = ''
}

const onSaveEdu = () => {
    emit('onSaveEdu')
}

const emit = defineEmits(['onSaveEdu'])

const onUploadFile = (type:string) => {
    uploadData.value.type = type
    if(type == 'cert'){
        uploadData.value.file = ruleForm.value.cert
    }
    if(type == 'degree_cert'){
        uploadData.value.file = ruleForm.value.degree_cert
    }
    if(type == 'network_cert'){
        uploadData.value.file = ruleForm.value.network_cert
    }
    
    uploadData.value.id = ruleForm.value.id
    showUpload.value = true
}

const saveUpload = (data:string) => {
    if(uploadData.value.type == 'cert'){
        ruleForm.value.cert = data
    }
    if(uploadData.value.type == 'degree_cert'){
        ruleForm.value.degree_cert = data
    }
    if(uploadData.value.type == 'network_cert'){
        ruleForm.value.network_cert = data
    }
    showUpload.value = false
}
</script>
<style scoped>
.el-tab-pane{height: calc(100vh - 25vh);overflow-y: scroll;}
::v-deep .el-form-item .el-form-item__label {
  font-size: 16px; /* 或者你想要的任何大小 */
}
::v-deep .el-tabs__item{font-size: 18px !important;}
</style>
